package com.cloudpan.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudpan.common.exception.GlobalExceptionHandler;
import com.cloudpan.entity.bo.SysFriendRequestBo;
import com.cloudpan.entity.pojo.SysFriendRequest;
import com.cloudpan.entity.pojo.SysUserFriend;
import com.cloudpan.entity.vo.SysAppealFriendListVo;
import com.cloudpan.mapper.SysFriendRequestMapper;
import com.cloudpan.mapper.SysUserFriendMapper;
import com.cloudpan.mapper.SysUserInfoMapper;
import com.cloudpan.service.SysFriendRequestService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Code Generator
 * @since 2023-11-15
 */
@Service
@RequiredArgsConstructor
public class SysFriendRequestServiceImpl extends ServiceImpl<SysFriendRequestMapper, SysFriendRequest> implements SysFriendRequestService {

    private final SysUserFriendMapper sysUserFriendMapper;

    private final SysUserInfoMapper sysUserInfoMapper;

    private final SysFriendRequestMapper sysFriendRequestMapper;

    /**
     * 申请添加好友
     * @param requestBo
     * @return
     */
    @Override
    public Boolean addFriend(SysFriendRequestBo requestBo) {
        LambdaQueryWrapper<SysFriendRequest> lambda = new LambdaQueryWrapper<>();
        lambda.eq(SysFriendRequest::getUserId,requestBo.getUserId())
                .eq(SysFriendRequest::getFriendId,requestBo.getFriendId());

        SysFriendRequest result = sysFriendRequestMapper.selectOne(lambda);
        if (!ObjectUtils.isEmpty(result)){
            throw new GlobalExceptionHandler("你以申请该好友的请求,不可再次申请");
        }

        if (requestBo.getUserId().equals(requestBo.getFriendId())){
            throw new GlobalExceptionHandler("无法添加自己为好友");
        }
        SysFriendRequest sysFriendRequest = new SysFriendRequest();
        //对象属性copy
        BeanUtils.copyProperties(requestBo, sysFriendRequest);
        return this.save(sysFriendRequest);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean reOrAg(SysFriendRequestBo requestBo) {
        if (StringUtils.isEmpty(requestBo.getId())){
            throw new GlobalExceptionHandler("好友请求id不能为空");
        }
        SysFriendRequest request = sysFriendRequestMapper.selectById(requestBo.getId());
        if (requestBo.getDoAgree().equals("1")) {
            request.setDoAgree("1");
            sysFriendRequestMapper.updateById(request);
            //用户
            SysUserFriend sysUserFriend = new SysUserFriend();
            sysUserFriend.setUserId(request.getUserId());
            sysUserFriend.setFriendId(request.getFriendId());
            sysUserFriend.setNotes(request.getNotes());
            sysUserFriendMapper.insert(sysUserFriend);
            //好友
            sysUserFriend.setId(null);
            sysUserFriend.setUserId(request.getFriendId());
            sysUserFriend.setFriendId(request.getUserId());
            sysUserFriend.setNotes(requestBo.getNotes());
            sysUserFriendMapper.insert(sysUserFriend);
            return Boolean.TRUE;
        }
        //拒绝好友
        request.setDoAgree("2");
        request.setRejectContent(!StringUtils.isNotBlank(requestBo.getRejectContent())?"":requestBo.getRejectContent());
        sysFriendRequestMapper.updateById(request);
        return Boolean.TRUE;
    }

    @Override
    public List<SysAppealFriendListVo> getAppeal(String userid) {
        return sysFriendRequestMapper.getAppeal(userid);
    }

}
